<?php

namespace App\Http\Controllers\Admin;

use App\Models\Admin;
use App\Models\Menu;

/**
 * 后台首页控制器
 */
class TestController extends BaseController
{

    public function __construct()
    {
        parent::__construct();
    }

    public function getIndex()
    {
        $url = 'http://q.song666.com/OA/WeChat/Trans?PageIndex=242&ids=163887';
        $html = file_get_contents($url);
        dd($html);
        return 'ok';
    }

    function login_post($url, $cookie, $post){
        $ch = curl_init(); //初始化curl模块
        curl_setopt($ch, CURLOPT_URL, $url); //登录提交的地址
        curl_setopt($ch, CURLOPT_HEADER, 0); //是否显示头信息
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); //是否自动显示返回的信息
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); //设置cookie信息保存在指定的文件夹中
        curl_setopt($ch, CURLOPT_POST, 1); //以POST方式提交
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));//要执行的信息
        curl_exec($ch); //执行CURL
        curl_close($ch);
    }


    public function getTest()
    {
        $curl = curl_init();
        $cookie_jar = tempnam(public_path(),'cookie');
        $url = 'http://q.song666.com/Account/Auth/Login';
        curl_setopt($curl, CURLOPT_URL,$url);
        curl_setopt($curl, CURLOPT_POST, 1);
        $post = array(
            'username' => '13027100530',
            'password' => '123456',
        );
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post);//传 递数据
        curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar);// 把返回来的cookie信息保存在$cookie_jar文件中
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_NOBODY, false);
        curl_exec($curl);
        curl_close($curl);

        $url = 'http://q.song666.com/OA/WeChat/Trans?PageIndex=242&ids=163887';

        $curl2 = curl_init();
        curl_setopt($curl2, CURLOPT_URL, $url);
        curl_setopt($curl2, CURLOPT_HEADER, false);
        curl_setopt($curl2, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl2, CURLOPT_COOKIEFILE, $cookie_jar);
        $response = curl_exec($curl2);
        $response = str_replace("\r\n",'',$response);
        $response = str_replace(' ','',$response);
        $patten = "/\<tr\>\<td\>.*\<\/td\>\<td\>(.*)\<\/td\>\<td\>(.*)\<\/td\>\<td\>(.*)\<\/td\>\<td\>(.*)\<\/td\>\<\/tr\>/";
        preg_match_all($patten, $response, $res);

        dd($res);
    }


    /**
     * @param $url 请求网址
     * @param bool $params 请求参数
     * @param int $ispost 请求方式
     * @param int $https https协议
     * @return bool|mixed
     */
    public function apiCurl($url='', $type = 0)
    {
//        tolog($url,'mobile.txt');
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        $response = curl_exec($ch);
//        dd($response);
        $patten = '/\/fuwu_dian\/(.*)\/songshui/';
        if ($type) {
            $patten = '/\@phone\=([0-9]{11})\@/';
        }
        preg_match_all($patten, $response, $links);
//        tolog($links,'aaa.txt');
        if (!empty($links)) {
            if ($type) {
                return substr($links[0][0], 7,11);
            }
            return $links[0];
        }
        return $links;
    }

    public function getContent($url='', $type = 0)
    {
       $html = file_get_contents($url);
//       dd($html);
        //如果出现中文乱码使用下面代码
//        $getcontent = iconv("gb2312", "utf-8",$html);
        $patten = '/\/fuwu_dian\/(.*)\/songshui/';
        if ($type) {
            $patten = '/\@phone\=([0-9]{11})\@/';
        }
        preg_match_all($patten, $html, $links);
//        tolog($links,'aaa.txt');
        if (!empty($links)) {
            if ($type) {
                return substr($links[0][0], 7,11);
            }
            return $links[0];
        }
        return $links;
    }


}
